home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / GDEVWDDB.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  18KB  |  615 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevwddb.c */
  20. /*
  21.  * Microsoft Windows 3.n driver for Ghostscript,
  22.  * using device-dependent bitmap.
  23.  * Original version by Russell Lang and Maurice Castro with help from
  24.  * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  25.  * created from gdevbgi.c and gnuplot/term/win.trm 5th June 1992.
  26.  * Extensively modified by L. Peter Deutsch, Aladdin Enterprises.
  27.  */
  28. #include "gdevmswn.h"
  29.  
  30. /* Make sure we cast to the correct structure type. */
  31. typedef struct gx_device_win_ddb_s gx_device_win_ddb;
  32. #undef wdev
  33. #define wdev ((gx_device_win_ddb *)dev)
  34.  
  35. /* Forward references */
  36. private void near win_addtool(P2(gx_device_win_ddb *, int));
  37. private void near win_maketools(P2(gx_device_win_ddb *, HDC));
  38. private void near win_destroytools(P1(gx_device_win_ddb *));
  39.  
  40. /* Device procedures */
  41.  
  42. /* See gxdevice.h for the definitions of the procedures. */
  43. private dev_proc_open_device(win_ddb_open);
  44. private dev_proc_close_device(win_ddb_close);
  45. private dev_proc_map_rgb_color(win_ddb_map_rgb_color);
  46. private dev_proc_fill_rectangle(win_ddb_fill_rectangle);
  47. private dev_proc_tile_rectangle(win_ddb_tile_rectangle);
  48. private dev_proc_copy_mono(win_ddb_copy_mono);
  49. private dev_proc_copy_color(win_ddb_copy_color);
  50. private dev_proc_draw_line(win_ddb_draw_line);
  51. /* Windows-specific procedures */
  52. private win_proc_copy_to_clipboard(win_ddb_copy_to_clipboard);
  53. private win_proc_repaint(win_ddb_repaint);
  54. private win_proc_alloc_bitmap(win_ddb_alloc_bitmap);
  55. private win_proc_free_bitmap(win_ddb_free_bitmap);
  56.  
  57. /* The device descriptor */
  58. struct gx_device_win_ddb_s {
  59.     gx_device_common;
  60.     gx_device_win_common;
  61.  
  62.     /* Handles */
  63.  
  64.     HBITMAP FAR hbitmap;
  65.     HDC FAR hdcbit;
  66.     HPEN hpen, *hpens;
  67.     uint hpensize;
  68.     HBRUSH hbrush, *hbrushs;
  69.     uint hbrushsize;
  70. #define select_brush(color)\
  71.   if (wdev->hbrush != wdev->hbrushs[color])\
  72.    {    wdev->hbrush = wdev->hbrushs[color];\
  73.     SelectObject(wdev->hdcbit,wdev->hbrush);\
  74.    }
  75.     HPALETTE hpalette;
  76.     LPLOGPALETTE lpalette;
  77.  
  78.     /* A staging bitmap for copy_mono. */
  79.     /* We want one big enough to handle the standard 16x16 halftone; */
  80.     /* this is also big enough for ordinary-size characters. */
  81.  
  82. #define bmWidthBytes 4        /* must be even */
  83. #define bmWidthBits (bmWidthBytes * 8)
  84. #define bmHeight 32
  85.     HBITMAP FAR hbmmono;
  86.     HDC FAR hdcmono;
  87.     gx_bitmap_id bm_id;
  88. };
  89. private gx_device_procs win_ddb_procs = {
  90.     win_ddb_open,
  91.     gx_default_get_initial_matrix,
  92.     win_sync_output,
  93.     win_output_page,
  94.     win_ddb_close,
  95.     win_ddb_map_rgb_color,
  96.     win_map_color_rgb,
  97.     win_ddb_fill_rectangle,
  98.     win_ddb_tile_rectangle,
  99.     win_ddb_copy_mono,
  100.     win_ddb_copy_color,
  101.     win_ddb_draw_line,
  102.     gx_default_get_bits,
  103.     gx_default_get_props,
  104.     win_put_props,
  105.     gx_default_map_cmyk_color,
  106.     win_get_xfont_procs
  107. };
  108. gx_device_win_ddb gs_mswin_device = {
  109.     sizeof(gx_device_win_ddb),
  110.     &win_ddb_procs,
  111.     "mswin",
  112.     INITIAL_WIDTH, INITIAL_HEIGHT,     /* win_open() fills these in later */
  113.     INITIAL_RESOLUTION, INITIAL_RESOLUTION,    /* win_open() fills these in later */
  114.     no_margins,
  115.     dci_black_and_white,
  116.     0,        /* not open yet */
  117.     2,        /* nColors */
  118.     win_ddb_copy_to_clipboard,
  119.     win_ddb_repaint,
  120.     win_ddb_alloc_bitmap,
  121.     win_ddb_free_bitmap
  122. };
  123.  
  124. /* Open the win_ddb driver */
  125. private int
  126. win_ddb_open(gx_device *dev)
  127. {    int code = win_open(dev);
  128.     HDC hdc;
  129.     if ( code < 0 ) return code;
  130.  
  131.     /* Create the backing bitmap. */
  132.     code = win_ddb_alloc_bitmap((gx_device_win *)dev, dev);
  133.     if ( code < 0 ) return code;
  134.  
  135.     /* Create the bitmap and DC for copy_mono. */
  136.     hdc = GetDC(wdev->hwndimg);
  137.     wdev->hbmmono = CreateBitmap(bmWidthBits, bmHeight, 1, 1, NULL);
  138.     wdev->hdcmono = CreateCompatibleDC(hdc);
  139.     if ( wdev->hbmmono == NULL || wdev->hdcmono == NULL )
  140.     {    win_ddb_free_bitmap((gx_device_win *)dev);
  141.         ReleaseDC(wdev->hwndimg, hdc);
  142.         return win_nomemory();
  143.     }
  144.     SetMapMode(wdev->hdcmono, GetMapMode(hdc));
  145.     SelectObject(wdev->hdcmono, wdev->hbmmono);
  146.     wdev->bm_id = gx_no_bitmap_id;
  147.     ReleaseDC(wdev->hwndimg, hdc);
  148.  
  149.     /* create palette and tools for bitmap */
  150.     if ((wdev->lpalette = win_makepalette((gx_device_win *)dev))
  151.         == (LPLOGPALETTE)NULL)
  152.         return win_nomemory();
  153.     wdev->hpalette = CreatePalette(wdev->lpalette);
  154.     (void) SelectPalette(wdev->hdcbit,wdev->hpalette,NULL);
  155.     RealizePalette(wdev->hdcbit);
  156.     win_maketools(wdev,wdev->hdcbit);
  157.  
  158.     wdev->hdctext = wdev->hdcbit;    /* draw text here */
  159.  
  160.     return 0;
  161. }
  162.  
  163. /* Close the win_ddb driver */
  164. private int
  165. win_ddb_close(gx_device *dev)
  166. {
  167.     /* Free resources */
  168.  
  169.     win_destroytools(wdev);
  170.     DeleteDC(wdev->hdcmono);
  171.     win_ddb_free_bitmap((gx_device_win *)dev);
  172.     DeleteObject(wdev->hpalette);
  173.     DeleteObject(wdev->hbmmono);
  174.     gs_free((char *)(wdev->lpalette), 1, sizeof(LOGPALETTE) + 
  175.         (1<<(wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  176.         "win_ddb_close");
  177.  
  178.     return win_close(dev);
  179. }
  180.  
  181. /* Map a r-g-b color to the colors available under Windows */
  182. private gx_color_index
  183. win_ddb_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  184.   gx_color_value b)
  185. {    int i = wdev->nColors;
  186.     gx_color_index color = win_map_rgb_color(dev, r, g, b);
  187.     LPLOGPALETTE lipal = wdev->limgpalette;
  188.     LPLOGPALETTE lpal = wdev->lpalette;
  189.     if ( color != i ) return color;
  190.  
  191.     /* We just added a color to the window palette. */
  192.     /* Add it to the bitmap palette as well. */
  193.  
  194.     DeleteObject(wdev->hpalette);
  195.     lpal->palPalEntry[i].peFlags = NULL;
  196.     lpal->palPalEntry[i].peRed   =  lipal->palPalEntry[i].peRed;
  197.     lpal->palPalEntry[i].peGreen =  lipal->palPalEntry[i].peGreen;
  198.     lpal->palPalEntry[i].peBlue  =  lipal->palPalEntry[i].peBlue;
  199.     lpal->palNumEntries = i+1;
  200.     wdev->hpalette = CreatePalette(lpal);
  201.     (void) SelectPalette(wdev->hdcbit,wdev->hpalette,NULL);
  202.     RealizePalette(wdev->hdcbit);
  203.     win_addtool(wdev, i);
  204.  
  205.     return color;
  206. }
  207.  
  208. /* Macro for filling a rectangle with a color. */
  209. /* Note that it starts with a declaration. */
  210. #define fill_rect(x, y, w, h, color)\
  211. RECT rect;\
  212. rect.left = x, rect.top = y;\
  213. rect.right = x + w, rect.bottom = y + h;\
  214. FillRect(wdev->hdcbit, &rect, wdev->hbrushs[(int)color])
  215.  
  216.  
  217. /* Fill a rectangle. */
  218. private int
  219. win_ddb_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  220.   gx_color_index color)
  221. {
  222.     fit_fill(dev, x, y, w, h);
  223.     /* Use PatBlt for filling.  Special-case black. */
  224.     if ( color == 0 )
  225.         PatBlt(wdev->hdcbit, x, y, w, h, rop_write_0s);
  226.     else
  227.     {    select_brush((int)color);
  228.         PatBlt(wdev->hdcbit, x, y, w, h, rop_write_pattern);
  229.     }
  230.     win_update((gx_device_win *)dev);
  231.  
  232.     return 0;
  233. }
  234.  
  235. /* Tile a rectangle.  If neither color is transparent, */
  236. /* pre-clear the rectangle to color0 and just tile with color1. */
  237. /* This is faster because of how win_copy_mono is implemented. */
  238. /* Note that this also does the right thing for colored tiles. */
  239. private int
  240. win_ddb_tile_rectangle(gx_device *dev, const gx_bitmap *tile,
  241.   int x, int y, int w, int h, gx_color_index czero, gx_color_index cone,
  242.   int px, int py)
  243. {    fit_fill(dev, x, y, w, h);
  244.     if ( czero != gx_no_color_index && cone != gx_no_color_index )
  245.        {    fill_rect(x, y, w, h, czero);
  246.         czero = gx_no_color_index;
  247.        }
  248.     if ( tile->raster == bmWidthBytes && tile->size.y <= bmHeight &&
  249.          (px | py) == 0 && cone != gx_no_color_index
  250.        )
  251.     {    /* We can do this much more efficiently */
  252.         /* by using the internal algorithms of copy_mono */
  253.         /* and gx_default_tile_rectangle. */
  254.         int width = tile->size.x;
  255.         int height = tile->size.y;
  256.         int rwidth = tile->rep_width;
  257.         int irx = ((rwidth & (rwidth - 1)) == 0 ? /* power of 2 */
  258.             x & (rwidth - 1) :
  259.             x % rwidth);
  260.         int ry = y % tile->rep_height;
  261.         int icw = width - irx;
  262.         int ch = height - ry;
  263.         int ex = x + w, ey = y + h;
  264.         int fex = ex - width, fey = ey - height;
  265.         int cx, cy;
  266.  
  267.         select_brush((int)cone);
  268.  
  269.         if ( tile->id != wdev->bm_id || tile->id == gx_no_bitmap_id )
  270.         {    wdev->bm_id = tile->id;
  271.             SetBitmapBits(wdev->hbmmono,
  272.                       (DWORD)(bmWidthBytes * tile->size.y),
  273.                       (BYTE *)tile->data);
  274.         }
  275.  
  276. #define copy_tile(srcx, srcy, tx, ty, tw, th)\
  277.   BitBlt(wdev->hdcbit, tx, ty, tw, th, wdev->hdcmono, srcx, srcy, rop_write_at_1s)
  278.  
  279.         if ( ch > h ) ch = h;
  280.         for ( cy = y; ; )
  281.            {    if ( w <= icw )
  282.                 copy_tile(irx, ry, x, cy, w, ch);
  283.             else
  284.             {    copy_tile(irx, ry, x, cy, icw, ch);
  285.                 cx = x + icw;
  286.                 while ( cx <= fex )
  287.                 {    copy_tile(0, ry, cx, cy, width, ch);
  288.                     cx += width;
  289.                 }
  290.                 if ( cx < ex )
  291.                 {    copy_tile(0, ry, cx, cy, ex - cx, ch);
  292.                 }
  293.             }
  294.             if ( (cy += ch) >= ey ) break;
  295.             ch = (cy > fey ? ey - cy : height);
  296.             ry = 0;
  297.            }
  298.  
  299.         win_update((gx_device_win *)dev);
  300.         return 0;
  301.     }
  302.     return gx_default_tile_rectangle(dev, tile, x, y, w, h, czero, cone, px, py);
  303. }
  304.  
  305.  
  306. /* Draw a line */
  307. private int
  308. win_ddb_draw_line(gx_device *dev, int x0, int y0, int x1, int y1,
  309.   gx_color_index color)
  310. {
  311.     if (wdev->hpen != wdev->hpens[(int)color]) {
  312.         wdev->hpen = wdev->hpens[(int)color];
  313.         SelectObject(wdev->hdcbit,wdev->hpen);
  314.     }
  315.     MoveTo(wdev->hdcbit, x0, y0);
  316.     LineTo(wdev->hdcbit, x1, y1);
  317.     return 0;
  318. }
  319.  
  320. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  321. /* Color = gx_no_color_index means transparent (no effect on the image). */
  322. private int
  323. win_ddb_copy_mono(gx_device *dev,
  324.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  325.   int x, int y, int w, int h,
  326.   gx_color_index zero, gx_color_index one)
  327. {    int endx;
  328.     const byte *ptr_line;
  329.     int width_bytes, height;
  330.     DWORD rop = rop_write_at_1s;
  331.     int color;
  332.     BYTE aBit[bmWidthBytes * bmHeight];
  333.     BYTE *aptr = aBit;
  334.  
  335.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  336.  
  337.     if ( sourcex & ~7 )
  338.     {    base += sourcex >> 3;
  339.         sourcex &= 7;
  340.     }
  341.  
  342.     /* Break up large transfers into smaller ones. */
  343.     while ( (endx = sourcex + w) > bmWidthBits )
  344.     {    int lastx = (endx - 1) & -bmWidthBits;
  345.         int subw = endx - lastx;
  346.         int code = win_ddb_copy_mono(dev, base, lastx,
  347.                          raster, gx_no_bitmap_id,
  348.                          x + lastx - sourcex, y,
  349.                          subw, h, zero, one);
  350.         if ( code < 0 ) return code;
  351.         w -= subw;
  352.     }
  353.     while ( h > bmHeight )
  354.     {    int code;
  355.         h -= bmHeight;
  356.         code = win_ddb_copy_mono(dev, base + h * raster, sourcex,
  357.                      raster, gx_no_bitmap_id,
  358.                      x, y + h, w, bmHeight, zero, one);
  359.         if ( code < 0 ) return code;
  360.     }
  361.  
  362.     width_bytes = (sourcex + w + 7) >> 3;
  363.     ptr_line = base;
  364.  
  365.     if ( zero == gx_no_color_index )
  366.        {    if ( one == gx_no_color_index ) return 0;
  367.         color = (int)one;
  368.         if ( color == 0 )
  369.             rop = rop_write_0_at_1s;
  370.         else
  371.             select_brush(color);
  372.        }
  373.     else
  374.        {    if ( one == gx_no_color_index )
  375.            {    color = (int)zero;
  376.             rop = rop_write_at_0s;
  377.            }
  378.         else
  379.            {    /* Pre-clear the rectangle to zero */
  380.             fill_rect(x, y, w, h, zero);
  381.             color = (int)one;
  382.            }
  383.         select_brush(color);
  384.        }
  385.  
  386.     if ( id != wdev->bm_id || id == gx_no_bitmap_id )
  387.     {    wdev->bm_id = id;
  388.         if ( raster == bmWidthBytes )
  389.         {    /* We can do the whole thing in a single transfer! */
  390.             SetBitmapBits(wdev->hbmmono,
  391.                       (DWORD)(bmWidthBytes * h),
  392.                       (BYTE *)base);
  393.         }
  394.         else
  395.         {    for ( height = h; height--;
  396.                   ptr_line += raster, aptr += bmWidthBytes
  397.                 )
  398.             {    /* Pack the bits into the bitmap. */
  399.                 switch ( width_bytes )
  400.                 {
  401.                     default: memcpy(aptr, ptr_line, width_bytes); break;
  402.                     case 4: aptr[3] = ptr_line[3];
  403.                     case 3: aptr[2] = ptr_line[2];
  404.                     case 2: aptr[1] = ptr_line[1];
  405.                     case 1: aptr[0] = ptr_line[0];
  406.                 }
  407.             }
  408.             SetBitmapBits(wdev->hbmmono,
  409.                       (DWORD)(bmWidthBytes * h),
  410.                       &aBit[0]);
  411.         }
  412.     }
  413.  
  414.     BitBlt(wdev->hdcbit, x, y, w, h, wdev->hdcmono, sourcex, 0, rop);
  415.     win_update((gx_device_win *)dev);
  416.     return 0;
  417. }
  418.  
  419.  
  420. /* Copy a color pixel map.  This is just like a bitmap, except that */
  421. /* each pixel takes 8 or 4 bits instead of 1 when device driver has color. */
  422. private int
  423. win_ddb_copy_color(gx_device *dev,
  424.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  425.   int x, int y, int w, int h)
  426. {
  427.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  428.  
  429.     if ( gx_device_has_color(dev) )
  430.     {
  431.     switch(dev->color_info.depth) {
  432.       case 8:
  433.         {    int xi, yi;
  434.         int skip = raster - w;
  435.         const byte *sptr = base + sourcex;
  436.           if ( w <= 0 ) return 0;
  437.           if ( x < 0 || x + w > dev->width )
  438.             return_error(gs_error_rangecheck);
  439.         for ( yi = y; yi - y < h; yi++ )
  440.            {
  441.             for ( xi = x; xi - x < w; xi++ )
  442.                {    int color =  *sptr++;
  443.                 SetPixel(wdev->hdcbit,xi,yi,PALETTEINDEX(color));
  444.                }
  445.             sptr += skip;
  446.            }
  447.         }
  448.         break;
  449.       case 4:
  450.        {    /* color device, four bits per pixel */
  451.         const byte *line = base + (sourcex >> 1);
  452.         int dest_y = y, end_x = x + w;
  453.  
  454.         if ( w <= 0 ) return 0;
  455.         while ( h-- )              /* for each line */
  456.            {    const byte *source = line;
  457.             register int dest_x = x;
  458.             if ( sourcex & 1 )    /* odd nibble first */
  459.                {    int color =  *source++ & 0xf;
  460.                 SetPixel(wdev->hdcbit,dest_x,dest_y,PALETTEINDEX(color));
  461.                 dest_x++;
  462.                }
  463.             /* Now do full bytes */
  464.             while ( dest_x < end_x )
  465.                {    int color = *source >> 4;
  466.                 SetPixel(wdev->hdcbit,dest_x,dest_y,PALETTEINDEX(color));
  467.                 dest_x++;
  468.                 if ( dest_x < end_x )
  469.                    {    color =  *source++ & 0xf;
  470.                     SetPixel(wdev->hdcbit,dest_x,dest_y,PALETTEINDEX(color));
  471.                     dest_x++;
  472.                    }
  473.                }
  474.             dest_y++;
  475.             line += raster;
  476.            }
  477.        }
  478.        break;
  479.     default:
  480.         return(-1); /* panic */
  481.     }
  482.     }
  483.     else 
  484.     /* monochrome device: one bit per pixel */
  485.        {    /* bitmap is the same as win_copy_mono: one bit per pixel */
  486.         win_ddb_copy_mono(dev, base, sourcex, raster, id, x, y, w, h,
  487.             (gx_color_index)0, 
  488.             (gx_color_index)(dev->color_info.depth==8 ? 63 : dev->color_info.max_gray));
  489.        }
  490.     win_update((gx_device_win *)dev);
  491.     return 0;
  492. }
  493.  
  494. /* ------ Windows-specific device procedures ------ */
  495.  
  496.  
  497. /* Copy the bitmap to the clipboard. */
  498. private void
  499. win_ddb_copy_to_clipboard(gx_device_win *dev)
  500. {    /* make somewhere to put it and copy */
  501.     HDC hdcbit = wdev->hdcbit;
  502.     HBITMAP bitmap = CreateCompatibleBitmap(hdcbit, dev->width,
  503.                         dev->height);
  504.     if (bitmap) {
  505.         /* there is enough memory and the bitmaps OK */
  506.         HDC mem = CreateCompatibleDC(hdcbit);
  507.         SelectObject(mem, bitmap);
  508.         BitBlt(mem, 0, 0, dev->width, dev->height,
  509.                hdcbit, 0, 0, SRCCOPY);
  510.         DeleteDC(mem);
  511.         /* copy it to the clipboard */
  512.         OpenClipboard(wdev->hwndimg);
  513.         EmptyClipboard();
  514.         SetClipboardData(CF_BITMAP, bitmap);
  515.         SetClipboardData(CF_PALETTE, CreatePalette(wdev->limgpalette));
  516.         CloseClipboard();
  517.     }
  518. }
  519.  
  520.  
  521. /* Repaint a section of the window. */
  522. private void
  523. win_ddb_repaint(gx_device_win *dev, HDC hdc, int dx, int dy, int wx, int wy,
  524.   int sx, int sy)
  525. {    BitBlt(hdc, dx, dy, wx, wy, wdev->hdcbit, sx, sy, SRCCOPY);
  526. }
  527.  
  528.  
  529. /* Allocate the backing bitmap. */
  530. private int
  531. win_ddb_alloc_bitmap(gx_device_win *dev, gx_device *param_dev)
  532. {
  533.     HDC hdc;
  534.     int i;
  535.  
  536.     hdc = GetDC(wdev->hwndimg);
  537.     for ( i = 0; ; i++ )
  538.     {    wdev->hbitmap = CreateCompatibleBitmap(hdc,
  539.                 param_dev->width, param_dev->height);
  540.         if ( wdev->hbitmap != (HBITMAP)NULL )
  541.             break;
  542.         if ( i >= 4 )
  543.         {    ReleaseDC(wdev->hwndimg, hdc);
  544.             return win_nomemory();
  545.         }
  546.         fprintf(stderr, "\nNot enough memory for bitmap.  Halving resolution... ");
  547.         param_dev->x_pixels_per_inch /= 2;
  548.         param_dev->y_pixels_per_inch /= 2;
  549.         param_dev->width /= 2;
  550.         param_dev->height /= 2;
  551.     }
  552.  
  553.     wdev->hdcbit = CreateCompatibleDC(hdc);  /* create Device Context for drawing */
  554.     SelectObject(wdev->hdcbit, wdev->hbitmap);
  555.     ReleaseDC(wdev->hwndimg, hdc);
  556.     return 0;
  557. }
  558.  
  559.  
  560. /* Free the backing bitmap. */
  561. private void
  562. win_ddb_free_bitmap(gx_device_win *dev)
  563. {    DeleteDC(wdev->hdcbit);        /* must do this first */
  564.     DeleteObject(wdev->hbitmap);
  565. }
  566.  
  567.  
  568. /* ------ Internal routines ------ */
  569.  
  570. #undef wdev
  571.  
  572.  
  573. private void near
  574. win_addtool(gx_device_win_ddb *wdev, int i)
  575. {
  576.     wdev->hpens[i] = CreatePen(PS_SOLID, 1, PALETTEINDEX(i));
  577.     wdev->hbrushs[i] = CreateSolidBrush(PALETTEINDEX(i));
  578. }
  579.  
  580.  
  581. private void near
  582. win_maketools(gx_device_win_ddb *wdev, HDC hdc)
  583. {    int i;
  584.     wdev->hpensize = (1<<(wdev->color_info.depth)) * sizeof(HPEN);
  585.     wdev->hpens = (HPEN *)gs_malloc(1, wdev->hpensize,
  586.                     "win_maketools(pens)");
  587.     wdev->hbrushsize = (1<<(wdev->color_info.depth)) * sizeof(HBRUSH);
  588.     wdev->hbrushs = (HBRUSH *)gs_malloc(1, wdev->hbrushsize,
  589.                         "win_maketools(brushes)");
  590.     if (wdev->hpens && wdev->hbrushs) {
  591.         for (i=0; i<wdev->nColors; i++)
  592.             win_addtool(wdev, i);
  593.  
  594.         wdev->hpen = wdev->hpens[0];
  595.         SelectObject(hdc,wdev->hpen);
  596.  
  597.         wdev->hbrush = wdev->hbrushs[0];
  598.         SelectObject(hdc,wdev->hbrush);
  599.     }
  600. }
  601.  
  602.  
  603. private void near
  604. win_destroytools(gx_device_win_ddb *wdev)
  605. {    int i;
  606.     for (i=0; i<wdev->nColors; i++) {
  607.         DeleteObject(wdev->hpens[i]);
  608.         DeleteObject(wdev->hbrushs[i]);
  609.     }
  610.     gs_free((char *)wdev->hbrushs, 1, wdev->hbrushsize,
  611.         "win_destroytools(brushes)");
  612.     gs_free((char *)wdev->hpens, 1, wdev->hpensize,
  613.         "win_destroytools(pens)");
  614. }
  615.